/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ControlEscolar.AdminMajor;

import com.ControlEscolar.Beans.Subject;
import com.ControlEscolar.Exceptions.AlreadyExistExp;
import com.ControlEscolar.Exceptions.ServiceExp;
import com.ControlEscolar.Services.SubjectService;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

/**
 *
 * @author Kossel
 */
public class AdminSubject extends MappingDispatchAction {

SubjectService service = new SubjectService();
ArrayList results=new ArrayList();

    public void add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{

          SubjectForm subjectForm = (SubjectForm) form;
          Subject aSubject = new Subject();
          aSubject.setSubjectKey(subjectForm.getSubjectID());
          aSubject.setSubjectName(subjectForm.getSubjectName());
          aSubject.setSubjectTotalHr(Integer.parseInt(subjectForm.getSubjectTotalHr()));
          aSubject.setSubjectCredits(Integer.parseInt(subjectForm.getSubjectCredits()));
          try {
          service.createSubject(aSubject);
          response.getWriter().print("success");
     }catch(AlreadyExistExp ex){
             response.getWriter().print("Ya existe una materia con la clave");
        }

    }

     public ActionForward viewAll(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{
          results=service.getSubjectList();
          SubjectForm addSubjectForm = (SubjectForm) form;
          addSubjectForm.setResults(results);
        return mapping.findForward("success");

    }

     public ActionForward getSubjectsList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{
         results=service.getSubjectList();
         HttpSession session = request.getSession();
         session.setAttribute("subjectList", results);
        return mapping.findForward("success");
    }

  
    public ActionForward getSubject(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{

        String subjectKey=request.getParameter("id");
         Subject subject = new Subject();
         SubjectForm subForm = (SubjectForm)form;
         subject = service.getSubject(subjectKey);
         subForm.setSubjectID(subject.getSubjectKey());
         subForm.setSubjectName(subject.getSubjectName());
         subForm.setSubjectCredits(subject.getSubjectCredits()+"");
         subForm.setSubjectTotalHr(subject.getSubjectTotalHr()+"");
        return mapping.findForward("success");
     
    }

        public void deleteSubject(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{

          String subjectKey=request.getParameter("id");
           try {
                service.deleteSubject(subjectKey);
                response.getWriter().print("success");
        }catch(ServiceExp ex){
             response.getWriter().print("No se puede eliminar esta materia, porque esta asignado en un plan de estudio");

        }
    }

   public void getSubjectJSON(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{

          String subjectKey=request.getParameter("id");
          Subject subject=new Subject();
          subject=service.getSubject(subjectKey);
          String json="{\"key\":\""+subject.getSubjectKey()
                  +"\", \"name\":\""+subject.getSubjectName()
                  +"\", \"credits\":"+subject.getSubjectCredits()
                  +", \"hr\":"+subject.getSubjectTotalHr()+"}";
          System.out.println(json);
          System.out.println("blabla " + subjectKey);
          response.getWriter().print(json);
    }

    public void modifySubject(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)throws Exception{
          Subject subject = new Subject();
          SubjectForm subjectForm=(SubjectForm)form;
          System.out.println("creditsss" + subjectForm.getSubjectCredits());
          subject.setSubjectKey(subjectForm.getSubjectID());
          subject.setSubjectName(subjectForm.getSubjectName());
          subject.setSubjectTotalHr(Integer.parseInt(subjectForm.getSubjectTotalHr()));
          subject.setSubjectCredits(Integer.parseInt(subjectForm.getSubjectCredits()));
           try {
                service.updateSubject(subject);
                response.getWriter().print("success");
        }catch(ServiceExp ex){
             response.getWriter().print("No se puede modificar esta materia, Verifique sus datos");

        }


    }


}
